clear all

ssc install synth

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_final"
global Output "Y:/limited/Michigan_CTE/funding_change/output/figures"

qui include "Y:/limited/Michigan_CTE/funding_change/code/analyze/figures/00_colors.do"

local demographics "female black hisp another_race sped lep econdis"
local academics "test_avg_g8 test_avg_sq_g8 test_avg_g8_imp attendance_g8 attendance_g8_imp"

// LOAD DATA
import excel "${Input}/ConcentratorCustomReport.xlsx", clear firstrow cellrange(A2:U686)

foreach v of varlist * {
	ren `v' `=lower("`v'")'
}
keep state year total

drop if regexm(state,"Total")
drop if inlist(state,"American Virgin Islands","District of Columbia","Guam","Palau","Puerto Rico")

gen school_year = real("20"+substr(year,-2,.))
encode state, gen(state_num)

merge m:1 state using  "${Input}/state_enr2014.dta", keep(3) nogen

* Cross-sectional concentrator rate:
*    (High School is 4/13 of total enrollment)
gen pct_conc = total/(enr*4/13)*100


*Flag crazy outlyer
tsset state_num school_year
gen change = d.pct_conc
bys state: gegen max_change = max(abs(change))

keep if school_year>=2010

preserve
forval x = 2010/2019 {
gen c`x' = school_year==`x' & state=="Michigan"
}
replace c2014 = 0

gen beta =.
gen up = .
gen lo = .
gen y =.
reghdfe pct_conc c2010-c2019 , a(state year) cluster(state)
forval i = 10/19 {
	replace beta = _b[c20`i'] in `i'
	replace up = _b[c20`i'] + 1.96 * _se[c20`i'] in `i'
	replace lo = _b[c20`i'] - 1.96 * _se[c20`i']	in `i'
	replace y = 20`i' in `i'
}

gen beta2 =.
gen up2 = .
gen lo2 = .
reghdfe pct_conc c2010-c2019 if max_change<10, a(state year) cluster(state)
forval i = 10/19 {
	replace beta2 = _b[c20`i'] in `i'
	replace up2 = _b[c20`i'] + 1.96 * _se[c20`i'] in `i'
	replace lo2 = _b[c20`i'] - 1.96 * _se[c20`i']	in `i'
}






// DRAW GRAPH
tw (rarea up lo y, color(gs10%20) lwidth(none)) ///
   (rarea up2 lo2 y, color("$c2%30") lwidth(none)) ///
   (connected beta beta2 y, sort ///
   scale(1.1) ///
   mc(gs10 "$c2" ) ///
   m(S T) ///
	 mfc(white "$c2") ///
   lc(gs10 "$c2") ///
   lp(solid longdash )), ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(13.6 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(14.4 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2010/2019) ///
   xtitle(" " "School Year (Spring)", margin(t=2)) ///
   ylabel(-5(5)15, angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax) ///
   ytitle("Percentage Point Change" " ") ///
   legend(order(3 "All States" 4 "States with Consistent Data") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7.5) ysize(3.25) ///
	 scheme(s2color)

// EXPORT GRAPH


graph save $Output/02b_corss_sectional_regression.gph, replace
*graph export $Output/02_reg_student_advancement.pdf, replace
restore



synth pct_conc pct_conc(2010) pct_conc(2011) pct_conc(2012) pct_conc(2013) pct_conc(2014) , trunit(22) trperiod(2015) keep(synth_results_data.dta, replace)

preserve
use synth_results_data.dta, clear
tw (connected _Y_treated _Y_synthetic _time) if _time!=.

tw (connected _Y_synthetic _Y_treated _time, sort ///
   scale(1.1) ///
   mc(gs10 "$c2" ) ///
   m(S T) ///
	 mfc(white "$c2") ///
   lc(gs10 "$c2") ///
   lp(solid longdash )), ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   text(19 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(19.4 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2010/2019) ///
   xtitle(" " "School Year (Spring)", margin(t=2)) ///
   ylabel(10(2)20, angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax) ///
   ytitle("Cross-Sectional Concentration Rate" " ") ///
   legend(order(1 "Synthetic Michigan" 2 "Michigan") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7.5) ysize(3.25) ///
	 scheme(s2color)



graph save $Output/A02b_synthetic_control.gph, replace	 
	 
restore


/* Alternative SYNTH
preserve
keep if max_change<10
synth pct_conc pct_conc(2010) pct_conc(2011) pct_conc(2012) pct_conc(2013) pct_conc(2014), trunit(22) trperiod(2015) keep(synth_results_data.dta) replace
use synth_results_data.dta, clear

tw (connected _Y_synthetic _Y_treated _time, sort ///
   scale(1.1) ///
   mc(gs10 "$c2" ) ///
   m(S D) ///
	 mfc(white "$c2") ///
   lc(gs10 "$c2") ///
   lp(solid longdash )), ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   text(19 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(19.4 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2010/2019) ///
   xtitle(" " "School Year (Spring)", margin(t=2)) ///
   ylabel(10(2)20, angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax) ///
   ytitle("Cross-Sectional Concentration Rate" " ") ///
   legend(order(1 "Synthetic Michigan" 2 "Michigan") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7.5) ysize(3.25) ///
	 scheme(s2color)


*placebo

cap mkdir placebo
keep if max2<.1
levelsof id if max2<.1, local(idlist)
foreach i in `idlist' {
	di "`i'"
	synth pct_conc pct_conc(2010) pct_conc(2011) pct_conc(2012) pct_conc(2013) pct_conc(2014), trunit(`i') trperiod(2015) keep(placebo/temp`i'.dta) replace
}
preserve
levelsof id if max2<.1, local(idlist)
clear
foreach i in `idlist'{
	use placebo/temp`i'.dta
	keep if _time!=.
	keep _Y_treated _Y_synthetic _time
	gen dif = _Y_treated - _Y_synthetic
	gen unit = `i'
	save placebo/clean`i'.dta, replace
}

clear
foreach i in `idlist' {
	append using placebo/clean`i'.dta
	local tw = "`tw' (line dif _time if unit==`i', lcolor(gs12) lw(.1))"
}
tw `tw' (line dif _time if unit==23, lcolor(black) lw(.8))
restore
*/
